<?php

/**
 * Name: 系统控制器controller
 * User: Wudean
 * Date: 2018/05/16
 */
namespace App\Http\Controllers\Mapi;
use App\Http\Controllers\Controller;
use App\Services\WxAppService;
use FFMpeg\FFMpeg;
use Illuminate\Http\Request;
use App\Api\Helper\ReturnMessage;
use mysql_xdevapi\Exception;

class SystemController extends Controller
{

    /**
     * @param Request $request
     * @return mixed
     * @name 公共上传文件
     * @author iHammer
     */
    public function  uploadFile(Request $request){
        $type_model = $request->get("type")?:"image";
        $errmg ='fail';
        $data['file'] = uploadToOss('file',$errmg,$type_model);
        if ($data['file'])
            $data['file'] = str_replace('https://file.ihammer.cn','',$data['file']);
            return ReturnMessage::successData($data);
        return ReturnMessage::success($errmg,1001);
    }

    /**
     * @param $file
     * @return string
     * @name 获取视频播放时长
     * @user IHammer
     */
    public static function getVideoTime($file){
        //当前域名
        $url = $_SERVER['SERVER_NAME'];

        if($url=="www.ihammer.cc"){ //测试服务器
            $ffmpeg = "/usr/bin/bin/ffmpeg";
        }else{//本地环境
            $ffmpeg = "ffmpeg";
        }

        return exec($ffmpeg." -i ".$file." 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//");

    }

    public function getQrCode(Request $request){
        try{
            header('content-type:image/jpg');
            $input = $request->input();
            if(empty($input['path'])){
                throw new Exception('小程序码跳转路径不能为空');
            }
            $path = $input['path'];
            unset($input['path']);
            $scene = '';
            foreach($input as $key => $value){
                $scene = $key.'='.$value.'&';
            }
            $scene = trim($scene,'&');
            $data = array();
            $data['scene'] = $scene;
            // 现在改成了直接在授权的时候绑定上级,所以直接进入的是login
            $data['page'] = $path;
            $wxappService = new WxAppService();
            $access_token = $wxappService->get_access_token();
            $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
            echo Request_url($url,json_encode($data));
//            return $wxappService->qrCode($input['scene'],$input['path']);
        }catch (\Exception $e){
            return ReturnMessage::success($e->getMessage(),1001);
        }
    }

    public function mergeMusic(Request $request){
        try {
            $beat = $request->input('beat');
            if (empty($beat)) {
                //后需要自定义Exception
                throw new \Exception('beat不能为空');
            }
            $music = $request->file('file');
            $ffmpeg = FFMpeg::create();
            $ffmpeg->driver();
            $audio = $ffmpeg->open( 'audio.mp3' );
            $waveform = $audio->waveform();
            $waveform->save( 'waveform.png' );
        }catch(\Exception $e){
            return ReturnMessage::success($e->getMessage(),1001);
        }
    }

}
